<?php

class EPrettyPhoto extends CComponent {
	const THEME_FACEBOOK = "facebook";
	const THEME_DARK_ROUNDED = "dark_rounded";
	const THEME_DARK_SQUARE = "dark_square";
	const THEME_LIGHT_ROUNDED = "light_rounded";
	const THEME_LIGHT_SQUARE = "liht_square";

	const PRETTY_SINGLE = 1; // create pretty for single links?
	const PRETTY_GALLERY = 2; // create pretty gallery?

	/**
	 * @brief retrieve the script file name
	 */
	protected static function scriptName($css=false) {
		return $css ? '/css/prettyPhoto.css' : 'jquery.prettyPhoto.js';
	}

	public static function register() {
		$cs = Yii::app()->clientScript;
		$cs->registerCoreScript('jquery');
		$assets = Yii::app()->extensionPath . '/EPrettyPhoto/';
		$aUrl = Yii::app()->getAssetManager()->publish($assets);
		$cs->registerScriptFile($aUrl . '/' . self::scriptName());
		$cs->registerCssFile($aUrl . self::scriptName(true));
	}

	public static function add($selector=".gallery a",
			$gallery=self::PRETTY_GALLERY, $theme=self::THEME_FACEBOOK, $opts=array()) {

		self::register();

		$opts['theme'] = $theme;
		$options = CJavaScript::encode($opts);
		$rel = 'prettyPhoto';
		if ($gallery == self::PRETTY_GALLERY) $rel .= '[' . time() . ']';
		$script = '
			$("' . $selector . '").attr("rel","' . $rel . '");
			$("a[rel^=\'prettyPhoto\']").prettyPhoto(' . $options . ');';
		$cs = Yii::app()->clientScript;
		$cs->registerScript(__CLASS__, $script, CClientScript::POS_READY);
	}

}
